﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimioAPI;
using SimioAPI.Extensions;
using System.Windows.Forms;


namespace WLC
{
    class WLCMoveInSequenceStepDefinition : IStepDefinition
    {
        #region IStepDefinition Members

        public IStep CreateStep(IPropertyReaders properties)
        {
            return new WLCMoveInSequenceStep(properties);
        }

        public void DefineSchema(IPropertyDefinitions schema)
        {
            IPropertyDefinition sequence = schema.AddElementProperty("WLCSequence", WLCSequenceElementDefinition.ID);
            sequence.Description = "The WLCSequence to move forward in.";
            sequence.Required = true;
            
            
            IPropertyDefinition orderSizeState = schema.AddStateProperty("OrderSizeState");
            orderSizeState.Description = "The state variable holding the size of the order.";
            orderSizeState.Required = true;
                                   
        }

        public int NumberOfExits
        {
            get { return 1; }
        }


        public string Name
        {
            get { return "MoveInSequence"; }
        }

        public string Description
        {
            get { return ""; }
        }

        public System.Drawing.Image Icon
        {
            get { return null; }
        }



        public Guid UniqueID
        {
            get { return new Guid("{C960912E-0535-11DF-8B1C-FC5956D89593}"); }
        }

        #endregion
    }


    class WLCMoveInSequenceStep : IStep
    {

        IElementProperty _wlcSequenceProp;
        IStateProperty _orderSizeStateProp;
       
        public WLCMoveInSequenceStep(IPropertyReaders bag)
        {
            _wlcSequenceProp = (IElementProperty)bag.GetProperty("WLCSequence");
            _orderSizeStateProp = (IStateProperty)bag.GetProperty("OrderSizeState");
           
        }

        #region IStep Members

        public ExitType Execute(IStepExecutionContext context)
        {
            WLCSequenceElement wlcSequence = (WLCSequenceElement) _wlcSequenceProp.GetElement(context);
            


            double orderSize = _orderSizeStateProp.GetState(context).StateValue;

                     
            int movedTo = wlcSequence.MoveNext(orderSize);

            context.ExecutionInformation.TraceInformation(String.Format("Moved in sequence to NodeID {0} ", movedTo));

            return ExitType.FirstExit;
        }

        #endregion

    }
}
